﻿using HR.Config;
using HR.IService;
using HR.Models;
using System.Linq;

namespace HR.Service
{
    public class SalaryItemService : ISalaryItemService
    {
        public readonly HRDBContext db;

        public SalaryItemService(HRDBContext db)
        {
            this.db = db;
        }

        public Task<int> SalaryItemAdd(config_public_char cpc)
        {
            return Task.Run(() =>
            {
                cpc.attribute_kind = "薪酬设置";
                db.config_public_chars.Add(cpc);
                return db.SaveChanges();
            });
        }

        public Task<int> SalaryItemDelete(short pbc_id)
        {
            return Task.Run(() =>
            {
                config_public_char? cpc = db.config_public_chars.Find(pbc_id);
                db.config_public_chars.Remove(cpc);
                return db.SaveChanges(); 
            });
        }

        public Task<Paging<config_public_char>> SalaryItemFill(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<config_public_char> list = db.config_public_chars.Where(cpc => cpc.attribute_kind == "薪酬设置").ToList();
                List<config_public_char> cpc = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();

                Paging<config_public_char> pg = new Paging<config_public_char>()
                {
                    rows = list.Count,
                    tabs = cpc
                };
                return pg;
            });
        }

        public Task<int> SalaryItemUpdate(config_public_char cpc)
        {
            return Task.Run(() =>
            {
                config_public_char? cfpc = db.config_public_chars.Find(cpc.pbc_id);
                cfpc.attribute_name = cpc.attribute_name;
                return db.SaveChanges();
            });
        }
    }
}
